//
//  HEManageSpecView.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/4.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEManageSpecView.h"

/// 标题栏高度（包括确定/取消，和年/月/日）
#define KAlterHeight KScreenHeight * 0.5

/// 主题颜色
#define kThemeColor [UIColor blueColor]

@interface HEManageSpecView ()

// 中间标题
@property (nonatomic, strong) UILabel *titleLabel;

// 内容
@property (nonatomic, strong) QMUITextView *textView;

// 蒙层视图
@property (nonatomic, strong) UIView *maskView;

// 弹出背景视图
@property (nonatomic, strong) UIView *alertView;

@property (nonatomic, strong) UILabel *protocolLb;

@property (nonatomic, strong) QMUIFillButton *finishBtn;

@end

@implementation HEManageSpecView



- (void)show {
     
    [self initUI];
     
    UIWindow *keyWindow = BRGetKeyWindow();
    [keyWindow addSubview:self];
    // 动画前初始位置
    CGRect rect = self.alertView.frame;
    rect.origin.y = self.bounds.size.height;
    self.alertView.frame = rect;
    // 弹出动画
    self.maskView.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y -= BR_BOTTOM_MARGIN + KAlterHeight;
        self.alertView.frame = rect;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)initUI {
    self.frame = [UIScreen mainScreen].bounds;
    // 设置子视图的宽度随着父视图变化
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self addSubview:self.maskView];
    
    [self addSubview:self.alertView];
    
    [self.alertView addSubview:self.titleLabel];

    [self.alertView addSubview:self.textView];
    
    [self.alertView addSubview:self.protocolLb];

    [self.alertView addSubview:self.finishBtn];

}

#pragma mark -
#pragma mark - 蒙层视图
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2f];
        // 设置子视图的大小随着父视图变化
        _maskView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _maskView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapMaskView:)];
        [_maskView addGestureRecognizer:myTap];
    }
    return _maskView;
}

#pragma mark - 弹框视图
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - KAlterHeight - BR_BOTTOM_MARGIN, self.bounds.size.width,  BR_BOTTOM_MARGIN + KAlterHeight)];
        _alertView.backgroundColor = [UIColor whiteColor];
        _alertView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        [_alertView hg_setCornerOnTopWithRadius:10];
    }
    return _alertView;
}

#pragma mark - 中间标题label
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, self.bounds.size.width, 44)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = DYFont(22);
        _titleLabel.textColor = CNavLightBlackColor;
        _titleLabel.text = @"管理规范";
    }
    return _titleLabel;
}

#pragma mark - 语音识别内容
- (QMUITextView *)textView {
    if (!_textView) {
        _textView = [QMUITextView new];
        // 限制可输入的字符长度
        _textView.maximumTextLength = 1500;
        _textView.font = DYFont(17);
        _textView.textColor = CNavLightBlackColor;
        _textView.text = @"一、为维护聊天室聊天秩序，创造良好交流环境，保障网友权益，根据我国有关网络法规和聊天室具体情况，特制订本条例。\n二、用户使用聊天室应以本条例为行为准则，自觉遵守。用户进入聊天室即意味着承诺受本守则约束。\n三、聊天室的发布、维护、程序修改由本平台负责，聊天室修改或中断服务不需对用户或任何第三方负责。\n四、聊天室管理人员依据本守则进行管理，本平台对本条例具有最终解释权。";
        _textView.placeholderColor = CNavSubTitleColor; // 自定义 placeholder 的颜色
//        _textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
        _textView.typingAttributes = @{NSFontAttributeName: DYFont(17),
                                           NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
        };
        _textView.scrollEnabled = YES;
        _textView.backgroundColor = KWhiteColor;
        _textView.editable = NO;
        [self.alertView addSubview:_textView];
        
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(10);
            make.left.equalTo(self.alertView).mas_offset(15);
            make.right.equalTo(self.alertView).mas_offset(-15);
//            make.height.mas_greaterThanOrEqualTo(80);
            make.bottom.equalTo(self.protocolLb.mas_top).offset(-10);
        }];
    }
    return _textView;
}

- (UILabel *)protocolLb {
    if (!_protocolLb) {
        _protocolLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, self.bounds.size.width, 44)];
        _protocolLb.backgroundColor = [UIColor clearColor];
        _protocolLb.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _protocolLb.font = DYFont(17);
        _protocolLb.textColor = CNavBgColor;
        _protocolLb.text = @"用户行为条例 >>";
        _protocolLb.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeProtocoClick)];
        [_protocolLb addGestureRecognizer:tap];
        [self.alertView addSubview:_protocolLb];

        [_protocolLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.finishBtn.mas_top).mas_offset(-10);
            make.left.equalTo(self.alertView).mas_offset(15);
            make.right.equalTo(self.alertView).mas_offset(-15);
        }];
    }
    return _protocolLb;
}

- (void)seeProtocoClick {
    NSURL * file = [NSURL URLWithString: @"https://www.honglutec.com/platform-rules/circle_rule.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
    webView.isShowCloseBtn = YES;
    webView.showPageTitle = YES;
    RootNavigationController *nav = [[RootNavigationController alloc] initWithRootViewController:webView];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [_vc presentViewController:nav animated:YES completion:nil];
}

- (QMUIFillButton *)finishBtn {
    if (!_finishBtn) {
        _finishBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
        _finishBtn.titleLabel.font = UIFontMake(16);
        [_finishBtn setTitle:@"知道了" forState:UIControlStateNormal];
        [self.alertView addSubview: _finishBtn];
        
        [_finishBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.alertView.mas_bottom).mas_offset(-30);
            make.width.offset(130);
            make.height.offset(40);
            make.centerX.equalTo(self.alertView);
        }];
    }
    return _finishBtn;
}

#pragma mark -

- (void)finishClick {
    [self dismiss];
}

#pragma mark - 点击蒙层视图事件
- (void)didTapMaskView:(UITapGestureRecognizer *)sender {
    [self dismiss];
}

#pragma mark - 关闭选择器视图
- (void)dismiss {
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y +=  BR_BOTTOM_MARGIN + KAlterHeight;
        self.alertView.frame = rect;
        self.maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


@end
